package com.example.gymlearning.service.impl;

import com.example.gymlearning.dto.KnowledgePointDTO;
import com.example.gymlearning.entity.KnowledgePoint;
import com.example.gymlearning.mapper.KnowledgePointMapper;
import com.example.gymlearning.service.KnowledgePointService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (KnowledgePoint)表服务实现类
 *
 * @author makejava
 * @since 2025-10-27 21:05:53
 */
@Service("knowledgePointService")
@Slf4j  // 添加这行
public class KnowledgePointServiceImpl implements KnowledgePointService {
    @Resource
    private KnowledgePointMapper knowledgePointMapper;




    @Override
    public KnowledgePoint getById(Long id) {
        // 直接调用现有的queryById方法
        return this.queryById(id);
    }
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public KnowledgePoint queryById(Long id) {
        return this.knowledgePointMapper.queryById(id);
    }

      /**
     * 新增数据
     *
     * @param knowledgePoint 实例对象
     * @return 实例对象
     */
    @Override
    public KnowledgePoint insert(KnowledgePoint knowledgePoint) {
        this.knowledgePointMapper.insert(knowledgePoint);
        return knowledgePoint;
    }

    /**
     * 修改数据
     *
     * @param knowledgePoint 实例对象
     * @return 实例对象
     */
    @Override
    public KnowledgePoint update(KnowledgePoint knowledgePoint) {
        this.knowledgePointMapper.update(knowledgePoint);
        return this.queryById(knowledgePoint.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.knowledgePointMapper.deleteById(id) > 0;
    }


    @Override
    public List<KnowledgePointDTO> getPointsByPackageId(Long packageId) {
        List<KnowledgePoint> points = knowledgePointMapper.selectByPackageId(packageId);

        return points.stream().map(point -> {
            KnowledgePointDTO dto = new KnowledgePointDTO();
            dto.setId(point.getId());
            dto.setTitle(point.getTitle());
            dto.setDescription(point.getDescription());
            dto.setBaseDuration(point.getBaseDuration());
            dto.setBaseExperience(point.getBaseExperience());
            dto.setDifficultyLevel(point.getDifficultyLevel());
            // 设置其他需要的字段
            return dto;
        }).collect(Collectors.toList());
    }

    @Override
    public KnowledgePointDTO getPointDetail(Long pointId) {
        KnowledgePoint point = knowledgePointMapper.selectById(pointId);
        return convertToDTO(point);
    }


    /**
     * 将KnowledgePoint转换为KnowledgePointDTO
     */
    private KnowledgePointDTO convertToDTO(KnowledgePoint point) {
        if (point == null) {
            return null;
        }

        KnowledgePointDTO dto = new KnowledgePointDTO();
        dto.setId(point.getId());
        dto.setClassId(point.getClassId());
        dto.setTitle(point.getTitle());
        dto.setDescription(point.getDescription());
        dto.setContentUrl(point.getContentUrl());
        dto.setBaseDuration(point.getBaseDuration());
        dto.setBaseExperience(point.getBaseExperience());
        dto.setDifficultyLevel(point.getDifficultyLevel());
        dto.setPrerequisiteIds(point.getPrerequisiteIds());
        dto.setOrderNum(point.getOrderNum());
        dto.setCreatedTime(point.getCreatedTime());
        dto.setKnowledgeType(point.getKnowledgeType());
        dto.setPackageId(point.getPackageId());
        dto.setPackageName(point.getPackageName());

        return dto;
    }


    public Long getPackageIdByPointId(Long pointId) {
        // 实现根据知识点ID获取知识包ID的逻辑
        KnowledgePoint point = queryById(pointId);
        return point != null ? point.getPackageId() : null;
    }



    public int getPointCountByPackage(Long packageId) {
        // 实现统计知识包内知识点数量的逻辑
        return knowledgePointMapper.countByPackageId(packageId);
    }

    // KnowledgePointServiceImpl.java
// 在KnowledgePointService接口中声明以下方法，然后在这里实现：

    @Override
    public void unlockNextPoint(Long pointId) {
        try {
            // 获取当前知识点信息
            KnowledgePoint currentPoint = queryById(pointId);
            if (currentPoint == null) return;

            // 查找下一个知识点ID
            Long nextPointId = knowledgePointMapper.findNextPointId(pointId);
            if (nextPointId != null) {
                // 这里应该调用用户进度服务来解锁下一个知识点
                // userProgressService.unlockPoint(userId, nextPointId);
                log.info("解锁下一个知识点: {}", nextPointId);
            }
        } catch (Exception e) {
            log.error("解锁下一个知识点失败: {}", e.getMessage(), e);
        }
    }

    @Override
    public Page<KnowledgePointDTO> getPointsByPackageIdWithPagination(Long packageId, int page, int size) {
        PageHelper.startPage(page, size);
        List<KnowledgePointDTO> points = knowledgePointMapper.selectPointsByPackageId(packageId);
        return (Page<KnowledgePointDTO>) points;
    }


}
